package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;

import view.NewWorkoutPlan0;
import view.templates.*;

public class NewWorkoutPlanListener0 implements ActionListener, ItemListener {
	private Controller controller = null;
	private NewWorkoutPlan0 newWorkoutPlan;

	public NewWorkoutPlanListener0(Controller controller) {
		this.controller = controller;
	}

	@Override
	public void itemStateChanged(ItemEvent arg0) {
		this.newWorkoutPlan = controller.getFrame().getNewWorkoutPlan0();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.newWorkoutPlan = controller.getFrame().getNewWorkoutPlan0();

		if (e.getSource().getClass() == MyJButton.class) {
			MyJButton b = (MyJButton) e.getSource();
			if (b == newWorkoutPlan.getWholeBodyButton()) {
				JCheckBox[] boxes = newWorkoutPlan.getBoxes();
				for (int i = 0; i < boxes.length; i++) {
					boxes[i].setSelected(true);
				}
			} else if (b == newWorkoutPlan.getChestButton()) {
				JCheckBox[] boxes = newWorkoutPlan.getBoxes();
				for (int i = 0; i < boxes.length; i++) {
					JCheckBox box = boxes[i];
					if (i == 1 || i == 3 || i == 5) {
						box.setSelected(true);
					} else {
						box.setSelected(false);
					}
				}
			} else if (b == newWorkoutPlan.getBackButton()) {
				JCheckBox[] boxes = newWorkoutPlan.getBoxes();
				for (int i = 0; i < boxes.length; i++) {
					JCheckBox box = boxes[i];
					if (i == 2 || i == 4 || i == 6) {
						box.setSelected(true);
					} else {
						box.setSelected(false);
					}
				}
			} else if (b == newWorkoutPlan.getLegsAbsButton()) {
				JCheckBox[] boxes = newWorkoutPlan.getBoxes();
				for (int i = 0; i < boxes.length; i++) {
					JCheckBox box = boxes[i];
					if (i == 0 || i == 6) {
						box.setSelected(true);
					} else {
						box.setSelected(false);
					}
				}
			} else if (b == newWorkoutPlan.getNextButton()) {
				if (newWorkoutPlan.setStepResults()) {
					controller.getFrame().initNewWorkoutScreen1();
				}
			} else if (b == newWorkoutPlan.getReturnButton()) {
				controller.getFrame().initHomeScreen();
			}
		}
	}

}
